﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExplodeGeometries.CApEXGExplodeGeometriesEnabler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExplodeGeometries;

internal class CApEXGExplodeGeometriesEnabler : ITxCommandEnabler
{
  private TxTypeFilter _typeFilter = new TxTypeFilter();

  public CApEXGExplodeGeometriesEnabler()
  {
    this._typeFilter.AddIncludedType(typeof (ITxComponent));
    this._typeFilter.AddIncludedType(typeof (ITxGroup));
    this._typeFilter.AddIncludedType(typeof (TxGeometry));
    this._typeFilter.AddIncludedType(typeof (TxKinematicLink));
  }

  private bool CheckEnable()
  {
    bool flag = false;
    TxObjectList filteredItems = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) this._typeFilter);
    if (((Collection<ITxObject>) filteredItems).Count > 0)
      flag = true;
    foreach (ITxObject itxObject in (Collection<ITxObject>) filteredItems)
    {
      if (!(itxObject is ITxComponent comp) || !this.IsValidComponent(comp))
      {
        ITxComponent parentComponent = this.GetParentComponent(itxObject);
        if (parentComponent == null)
        {
          flag = false;
          break;
        }
        if (comp == null && parentComponent.RepresentationType != 6)
        {
          flag = false;
          break;
        }
      }
      else if (comp.RepresentationType != 6)
      {
        flag = false;
        break;
      }
    }
    return flag;
  }

  private bool IsValidComponent(ITxComponent comp)
  {
    return comp != null && ((ITxObject) comp).CanBeModified && !TxEngineeringDataInternal.ObjectIsSectionCut((ITxObject) comp) && !(comp is TxCable);
  }

  private ITxComponent GetParentComponent(ITxObject obj)
  {
    ITxObject comp = (ITxObject) obj.Collection;
    while (true)
    {
      switch (comp)
      {
        case null:
        case ITxComponent _:
          goto label_3;
        default:
          comp = (ITxObject) comp.Collection;
          continue;
      }
    }
label_3:
    if (!this.IsValidComponent(comp as ITxComponent))
      comp = (ITxObject) null;
    return comp as ITxComponent;
  }

  public bool Enable => this.CheckEnable();
}
